import {addEmployeeAPI, getAllEmployeeAPI, getEmployeesByPortAPI, modEmployeeAPI} from "@/network/employee/employee";
import {ElMessage} from "element-plus";
import {defineStore} from "pinia";
import {ref} from "vue";

export const useEmployeeStore = defineStore('employee', ()=>{
  const employeeList = ref([]);
  const totalCount = ref(0)
  const totalPages = ref(0)
  const getEmployeeLoading = ref(false)
  const employeeDrawerLoading = ref(false)
  //这里不要使用箭头函数，否则为employee_list赋值时，会提示this指向型错误，this会指向undefined
  function getEmployeeList(data) {
    getEmployeeLoading.value = true
    if(!data){
      data={
        skip: 1,
        limit: 10,
      }
    }
    getAllEmployeeAPI(data).then(res=>{
      getEmployeeLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0 && res.data.data){
        totalCount.value = res.data.total_count
        totalPages.value = res.data.total_pages
        employeeList.value = res.data.data
      }
    }).catch(error=>{
      ElMessage.error(error.message)
    })
  }

  function addEmployee(data){
    employeeDrawerLoading.value = true
    addEmployeeAPI(data).then(res=>{
      employeeDrawerLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0){
        this.toggleDrawer()
        this.getEmployeeList()
        return ElMessage.success("添加成功")
      }
    }).catch(error=>{
      return ElMessage.error(error.message)
    })
  }

  function modEmployee(data){
    employeeDrawerLoading.value = true
    modEmployeeAPI(data).then(res=>{
      employeeDrawerLoading.value = false
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0){
        this.toggleDrawer()
        this.getEmployeeList()
        return ElMessage.success("修改成功")
      }
    }).catch(error=>{
      return ElMessage.error(error.message)
    })
  }

  const drawer = ref(false);
  const employeeInfo = ref({
    phone: '',
    name: '',
    sex: '',
    port: '',
    email: '',
    entry_date: '',
    leave_date: '',
    status: '',
  })
  function toggleDrawer (data) {
    if(data && data !== {}){
      this.employeeInfo = {...data}
    }
    this.drawer = !this.drawer
  }

  const saleList = ref([])
  function getAllSales(){
    getEmployeesByPortAPI(3).then(res=>{
      if(!res){
        return ElMessage.error("未收到返回")
      }

      if(res.status !== 200){
        return ElMessage.error(res.message?res.message:res.status)
      }

      if(!res.data){
        return ElMessage.error("返回的数据为空")
      }

      if(res.data.code !== 0) {
        return ElMessage.error(res.data.message)
      }

      if(res.data.code===0 && res.data.data){
        saleList.value = res.data.data
      }
    }).catch(error=>{
      console.log("error", error)
      ElMessage.error(error.message)
    })
  }
  return {
    employeeList,
    totalCount,
    totalPages,
    getEmployeeList,
    getEmployeeLoading,
    addEmployee,
    modEmployee,
    employeeDrawerLoading,
    drawer,
    employeeInfo,
    toggleDrawer,
    saleList,
    getAllSales,
  }
})